home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / cdev / clrnxtwd.sit / Draw WDEF.c next >
Text File  |  1989-11-10  |  12KB  |  521 lines

  1. /*
  2.     WDEF Draw.c
  3.     
  4. */
  5.  
  6. #include <Color.h>
  7. #include <FontMgr.h>
  8. #include <MacTypes.h>
  9. #include <MemoryMgr.h>
  10. #include <OSUtil.h>
  11. #include <Quickdraw.h>
  12. #include <ToolboxUtil.h>
  13. #include <WindowMgr.h>
  14. #include <Global.h>
  15.  
  16. typedef struct {
  17.     Rect        stdState, userState;
  18.     Boolean        buttonState;
  19. } WSDRecord, *WSDPointer, **WSDHandle;
  20.  
  21. #define buttonState    (**(WSDHandle)(*(WindowPeek)window).dataHandle).buttonState
  22.  
  23. #define plain                0                /* used to set a text face to plain text */
  24. #define blackHex            0xFFFFFFFF
  25. #define gray1Hex            0x55AA55AA
  26. #define gray2Hex            0xAA55AA55
  27. #define whiteHex            0x00000000
  28.  
  29. typedef struct {
  30.     long        black[2], white[2], gray[2];
  31.     RGBColor    cwhite, clight, cgray, cdark, cblack, chilite, ctext;
  32. } patsRec, *patsPtr;
  33.  
  34. RGBColor PackGray(brightness)
  35.     int            brightness;
  36. {
  37.     RGBColor    gray;
  38.     
  39.     gray.red = gray.green = gray.blue = brightness;
  40.     return gray;
  41. }
  42.  
  43. /*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  44.             drawing my window
  45. ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
  46. drawMyWindow( var, window, param )
  47.     short        var;        /* variation code on this type window */
  48.     WindowPtr    window;            /* pointer to the window */
  49.     long        param;        /* parameter used for many things */
  50. {
  51.     SysEnvRec    thisWorld;
  52.     OSErr        err;
  53.     GrafPtr        wport, portSave;
  54.     Boolean        hasColorQD;
  55.     
  56.     PenState    savedPen;
  57.     PixPatHandle    pnPatSave, bkPatSave, fillPatSave;
  58.     RGBColor    pnClrSave, bkClrSave;
  59.     RgnHandle    clipSave, tempRgn;
  60.  
  61.     patsRec        patstor;
  62.     patsPtr        pat;
  63.     
  64.     Rect        windowRect;
  65.     GDHandle    gdh;
  66.     CTabHandle    colors;
  67.     
  68.     if ( !(*(WindowPeek)window).visible ) return;
  69.     
  70.     pat = &patstor;
  71.     
  72.     err = SysEnvirons( 1, &thisWorld );        /* assess if we have a color comp */
  73.     if (err != noErr) hasColorQD = false;
  74.     else hasColorQD = thisWorld.hasColorQD;
  75.     
  76.     if (hasColorQD) {
  77.         GetPort(&portSave);
  78.         GetCWMgrPort(&wport);
  79.         SetPort(wport);
  80.         
  81.         pnPatSave = NewPixPat();
  82.         bkPatSave = NewPixPat();
  83.         fillPatSave = NewPixPat();
  84.         
  85.         CopyPixPat(((CGrafPtr)wport)->pnPixPat, pnPatSave);
  86.         CopyPixPat(((CGrafPtr)wport)->bkPixPat, bkPatSave);
  87.         CopyPixPat(((CGrafPtr)wport)->fillPixPat, fillPatSave);
  88.         GetForeColor(&pnClrSave);
  89.         GetBackColor(&bkClrSave);
  90.         
  91.         pat->cblack = PackGray(0);
  92.         pat->cdark = PackGray(0x4000);
  93.         pat->cgray = PackGray(0x8000);
  94.         pat->clight = PackGray(0xc000);
  95.         pat->cwhite = PackGray(0xffff);
  96.         
  97.         if ((window->portBits.rowBytes & (3<<14)) == 0xc000) {
  98.             GetAuxWin(window, &colors);
  99.             pat->chilite = (**(GrafVars**)((CGrafPtr)window)->grafVars)
  100.                 .rgbHiliteColor;
  101.             pat->ctext = (**colors).ctTable[2].rgb;
  102.         } else {
  103.             pat->chilite = pat->cgray;
  104.             pat->ctext = pat->cblack;
  105.         }
  106.     }
  107.     
  108.     clipSave = NewRgn();
  109.     tempRgn = NewRgn();
  110.     GetClip(clipSave);
  111.     windowRect = (**(*(WindowPeek)window).strucRgn).rgnBBox;
  112.         
  113.     if ( Abs(windowRect.left % 2) == Abs(windowRect.top % 2) )
  114.         pat->gray[0] = pat->gray[1] = gray1Hex;
  115.     else
  116.         pat->gray[0] = pat->gray[1] = gray2Hex;
  117.     
  118.     pat->black[0] = pat->black[1] = blackHex;
  119.     pat->white[0] = pat->white[1] = whiteHex;
  120.     GetPenState( &savedPen );
  121.     
  122.     if (hasColorQD) {
  123.         gdh = GetDeviceList();
  124.         do {
  125.             if (TestDeviceAttribute(gdh, 13) && TestDeviceAttribute(gdh, 15)) {
  126.                 HLock(gdh);
  127.                 if (RectInRgn(&(**gdh).gdRect, clipSave)) {
  128.                     RectRgn( tempRgn, &(**gdh).gdRect );
  129.                     SectRgn(clipSave, tempRgn, tempRgn);
  130.                     SetClip(tempRgn);
  131.                     if ((**(**gdh).gdPMap).cmpSize >= 4)
  132.                         drawonce(param, var, window, windowRect,
  133.                             pat, true);
  134.                     else
  135.                         drawonce(param, var, window, windowRect,
  136.                             pat, false);
  137.                 }
  138.                 HUnlock(gdh);
  139.             }
  140.         } while ((gdh = GetNextDevice(gdh)) != nil);
  141.     } else
  142.         drawonce(param, var, window, windowRect, pat, false);
  143.     
  144.     SetClip(clipSave);
  145.     DisposeRgn(clipSave);
  146.     DisposeRgn(tempRgn);
  147.     
  148.     SetPenState(&savedPen);
  149.     if (hasColorQD) {
  150.         RGBForeColor(&pnClrSave);
  151.         RGBBackColor(&bkClrSave);
  152.         
  153.         CopyPixPat(pnPatSave, ((CGrafPtr)wport)->pnPixPat);
  154.         CopyPixPat(bkPatSave, ((CGrafPtr)wport)->bkPixPat);
  155.         CopyPixPat(fillPatSave, ((CGrafPtr)wport)->fillPixPat);
  156.         DisposPixPat(pnPatSave);
  157.         DisposPixPat(bkPatSave);
  158.         DisposPixPat(fillPatSave);
  159.         
  160.         SetPort(portSave);
  161.     }
  162.     
  163. } /* end of drawMyWindow() */
  164.  
  165. DrawPane(pat, thisRect, invert, drawColor)
  166.     patsPtr        pat;
  167.     Rect        thisRect;
  168.     Boolean        invert;
  169.     Boolean        drawColor;
  170. {
  171.     /* fill the area with white */
  172.     if (invert)
  173.         PenPat( &pat->black );
  174.     else
  175.         PenPat( &pat->white );
  176.     PaintRect( &thisRect );
  177.     /* add the bottom shadow */
  178.     PenSize( 1, 1 );
  179.     if (drawColor) {
  180.         PenPat( &pat->black );
  181.         if (invert)
  182.             RGBForeColor(&pat->cdark);
  183.         else
  184.             RGBForeColor(&pat->clight);
  185.     } else {
  186.         if (invert)
  187.             PenPat(&pat->black);
  188.         else
  189.             PenPat(&pat->white);
  190.     }
  191.     MoveTo(thisRect.left+1, thisRect.bottom-1);
  192.     LineTo(thisRect.right-1, thisRect.bottom-1);
  193.     LineTo(thisRect.right-1, thisRect.top+1);
  194.     /* add the top shadow */
  195.     if (drawColor) {
  196.         PenPat( &pat->black );
  197.         if (invert)
  198.             RGBForeColor(&pat->clight);
  199.         else
  200.             RGBForeColor(&pat->cdark);
  201.     } else {
  202.         if (invert)
  203.             PenPat(&pat->white);
  204.         else
  205.             PenPat(&pat->black);
  206.     }
  207.     MoveTo( thisRect.left, thisRect.bottom-1 );
  208.     LineTo( thisRect.left, thisRect.top );
  209.     LineTo( thisRect.right-1, thisRect.top );
  210. }
  211.  
  212. drawWindowTitle( window, titleRect, pat, hilited, drawColor )
  213.     WindowPtr    window;
  214.     Rect        *titleRect;
  215.     patsPtr        pat;
  216.     Boolean        hilited, drawColor;
  217. {
  218.     short        savedFont;
  219.     short        savedSize;
  220.     Style        savedFace;
  221.     Str255        windowTitle;
  222.     short        titleWidth;
  223.     RgnHandle    oldClip, newClip;
  224.     GrafPtr        aPort;
  225.     
  226.     GetPort(&aPort);
  227.     savedFont = aPort->txFont;
  228.     savedSize = aPort->txSize;
  229.     savedFace = aPort->txFace;
  230.  
  231.     TextFont( 3 );                                /* set geneva */
  232.     TextSize( 9 );                                /* and small */
  233.     TextFace( plain );                            /* and plain or italic */
  234.     
  235.     GetWTitle( window, windowTitle );
  236.     
  237.     titleWidth = StringWidth( windowTitle );
  238.     
  239.     if ( titleWidth > 0 ) {
  240.         
  241.         titleWidth += 12;                        /* enough for edges */
  242.     
  243.         if ( ( (*titleRect).right - (*titleRect).left ) > titleWidth )
  244.             (*titleRect).right = (*titleRect).left + titleWidth;
  245.         
  246.         if (hilited) DrawPane(pat, *titleRect, false, drawColor);
  247.         
  248.         /* now shrink the box & set the clip region in case of a long title */
  249.         oldClip = NewRgn();
  250.         newClip = NewRgn();
  251.         GetClip( oldClip );
  252.         
  253.         InsetRect( titleRect, 1, 1 );
  254.         RectRgn( newClip, titleRect );
  255.         SectRgn( oldClip, newClip, newClip );
  256.         SetClip( newClip );
  257.         
  258.         /* now position ourselves & draw the title */
  259.         PenPat(&pat->black);
  260.         if (drawColor) {
  261.             if (hilited)
  262.                 RGBForeColor(&pat->ctext);
  263.             else
  264.                 RGBForeColor(&pat->cwhite);
  265.         }
  266.         
  267.         MoveTo( (*titleRect).left + 5, (*titleRect).bottom - 2 );
  268.         DrawString( windowTitle );
  269.         
  270.         SetClip( oldClip );
  271.         DisposeRgn( oldClip );
  272.         DisposeRgn( newClip );
  273.                 
  274.     } /* enough of a title to go with */
  275.     
  276.     TextFont(savedFont);
  277.     TextSize(savedSize);
  278.     TextFace(savedFace);
  279. }
  280.  
  281. ToggleGoaway(frame, pat, hilite, drawColor)
  282. Rect        frame;
  283. patsPtr        pat;
  284. Boolean        hilite;
  285. Boolean        drawColor;
  286. {
  287.     DrawPane(pat, frame, hilite, drawColor);
  288.     InsetRect(&frame, 2, 2);
  289.     PenPat(&pat->black);
  290.     
  291.     if (hilite) {
  292.         if (drawColor)
  293.             RGBForeColor(&pat->chilite);
  294.         else
  295.             PenPat(&pat->white);
  296.     } else {
  297.         if (drawColor)
  298.             RGBForeColor(&pat->cblack);
  299.     }
  300.     PaintOval(&frame);
  301. }
  302.  
  303. ToggleZoom(frame, pat, hilite, drawColor)
  304. Rect        frame;
  305. patsPtr        pat;
  306. Boolean        hilite;
  307. Boolean        drawColor;
  308. {
  309.     DrawPane(pat, frame, hilite, drawColor);
  310.     InsetRect(&frame, 2, 2);
  311.     --frame.right; --frame.bottom;
  312.     PenPat(&pat->black);
  313.     
  314.     if (hilite) {
  315.         if (drawColor)
  316.             RGBForeColor(&pat->chilite);
  317.         else
  318.             PenPat(&pat->white);
  319.     } else {
  320.         if (drawColor)
  321.             RGBForeColor(&pat->cblack);
  322.     }
  323.     PaintRect(&frame);
  324. }
  325.  
  326. DrawFrame(pat, frame, width, drawColor)
  327.     patsPtr    pat;
  328.     Rect    frame;
  329.     int        width;
  330.     Boolean    drawColor;
  331. {
  332.     frame.right -= 2;
  333.     frame.bottom -= 2;
  334.     
  335.     PenSize(width, width);
  336.     if (drawColor)
  337.         RGBForeColor(&pat->clight);
  338.     else
  339.         PenPat( &pat->white );
  340.     FrameRect(&frame);
  341.     
  342.     OffsetRect(&frame, 2, 2);
  343.     if (drawColor)
  344.         RGBForeColor(&pat->cdark);
  345.     else
  346.         PenPat( &pat->black );
  347.     FrameRect(&frame);
  348.  
  349.     OffsetRect(&frame, -1, -1);
  350.     if (drawColor)
  351.         RGBForeColor(&pat->cgray);
  352.     else
  353.         PenPat( &pat->gray );
  354.     FrameRect( &frame );
  355. }
  356.  
  357. drawonce( param, var, window, windowRect, pat, drawColor)
  358.     long        param;
  359.     short        var;
  360.     WindowPtr    window;
  361.     Rect        windowRect;
  362.     patsPtr        pat;
  363.     Boolean        drawColor;
  364. {
  365.     Rect        thisRect;
  366.  
  367.     switch ( (short)param ) {
  368.     case ( 0 ):
  369.         
  370.         thisRect = windowRect;
  371.  
  372.         PenNormal();
  373.         
  374.         /* ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ now for the more specific parts */
  375.         switch ( var ) {
  376.         case ( documentProc ):                    /* a regular window */
  377.         case ( documentProc + 8):                /* a zooming window */
  378.         case ( noGrowDocProc ):                    /* a regular window with no growing */
  379.             DrawFrame(pat, thisRect, 2, drawColor);
  380.             InsetRect(&thisRect, 1,1);
  381.             
  382.             thisRect.bottom = thisRect.top + 19;
  383.             PenPat(&pat->black);
  384.             if (drawColor)
  385.                 RGBForeColor(&pat->cgray);
  386.             else
  387.                 PenPat( &pat->gray );
  388.             PaintRect( &thisRect );
  389.             PenPat( &pat->black );
  390.             if (drawColor)
  391.                 RGBForeColor(&pat->cdark);
  392.             PenSize( 1, 1 );
  393.             MoveTo( windowRect.left+3, windowRect.top+19 );
  394.             LineTo( windowRect.right-4, windowRect.top+19 );
  395.  
  396.             if ( (*(WindowPeek)window).hilited ) {
  397.                 /* ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ the goaway box */
  398.                 if ( (*(WindowPeek)window).goAwayFlag ) {
  399.                     SetRect( &thisRect, 
  400.                         windowRect.left     + 3, 
  401.                         windowRect.top         + 3,
  402.                         windowRect.left     + 17,
  403.                         windowRect.top         + 17
  404.                     );
  405.                     DrawPane(pat, thisRect, false, drawColor);
  406.                     /* add the symbol */
  407.                     ToggleGoaway(thisRect, pat, false, drawColor);
  408.                 }
  409.                 /* ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ the zoom box */
  410.                 if ( (*(WindowPeek)window).spareFlag ) {
  411.                     SetRect( &thisRect, 
  412.                         windowRect.right     - 17, 
  413.                         windowRect.top         + 3,
  414.                         windowRect.right     - 3,
  415.                         windowRect.top         + 17
  416.                     );
  417.                     DrawPane(pat, thisRect, false, drawColor);
  418.                     /* add the symbol */
  419.                     ToggleZoom(thisRect, pat, false, drawColor);
  420.                 }
  421.                 /* ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ the title */
  422.                 SetRect( &thisRect, 
  423.                     windowRect.left     + 19, 
  424.                     windowRect.top         + 3,
  425.                     windowRect.right     - 19,
  426.                     windowRect.top         + 17
  427.                 );
  428.                 PenSize( 1, 1 );
  429.                 if ( thisRect.right - thisRect.left > 0 )
  430.                     drawWindowTitle( window, &thisRect, pat, true, drawColor );
  431.             } else {
  432.                 /* ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ the blank title bar */
  433.                 if (!drawColor) {
  434.                     thisRect.top += 2;
  435.                     thisRect.left += 2;
  436.                     thisRect.bottom = thisRect.top + 14;
  437.                     thisRect.right -=2;
  438.                     DrawPane(pat, thisRect, false, drawColor);
  439.                 }
  440.                 /* ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ the title */
  441.                 SetRect( &thisRect, 
  442.                     windowRect.left     + 19, 
  443.                     windowRect.top         + 3,
  444.                     windowRect.right     - 19,
  445.                     windowRect.top         + 17
  446.                 );
  447.                 drawWindowTitle( window, &thisRect, pat, false, drawColor );
  448.             }
  449.             break;
  450.         
  451.         case ( dBoxProc ):                        /* dialog box variation */
  452.             DrawFrame(pat, thisRect, 4, drawColor);
  453.             thisRect.right -= 2;
  454.             thisRect.bottom -= 2;
  455.             OffsetRect(&thisRect, 1, 1);
  456.             InsetRect( &thisRect, 5, 5 );
  457.             PenSize( 2, 2 );
  458.             if (drawColor) {
  459.                 PenPat(&pat->black);
  460.                 RGBForeColor(&pat->clight);
  461.             } else
  462.                 PenPat( &pat->white );
  463.             FrameRect( &thisRect );
  464.             break;
  465.         case ( plainDBox ):                        /* plain box variation */
  466.             FrameRect( &thisRect );
  467.             break;
  468.         case ( altDBoxProc ):                    /* shadow box variation */
  469.             thisRect.right -= 2;
  470.             thisRect.bottom -= 2;
  471.             FrameRect( &thisRect );
  472.             PenSize( 2, 2 );
  473.             if (drawColor)
  474.                 RGBForeColor(&pat->cgray);
  475.             MoveTo( thisRect.left + 2, thisRect.bottom );
  476.             LineTo( thisRect.right, thisRect.bottom );
  477.             LineTo( thisRect.right, thisRect.top + 2 );
  478.             break;
  479.         }
  480.         
  481.         break;
  482.         
  483.     case ( wInGoAway ):
  484.     
  485.         SetRect( &thisRect,                         /* just toggle the goAway */
  486.             windowRect.left     + 3, 
  487.             windowRect.top         + 3,
  488.             windowRect.left     + 17,
  489.             windowRect.top         + 17
  490.         );
  491.         buttonState = !buttonState;
  492.         if (drawColor)
  493.             ToggleGoaway(thisRect, pat, buttonState, drawColor);
  494.         else
  495.             InvertRect(&thisRect);
  496.         
  497.         break;
  498.         
  499.     case ( wInZoomIn ):
  500.     case ( wInZoomOut ):
  501.  
  502.         SetRect( &thisRect,                         /* just toggle the zoomBox */
  503.             windowRect.right     - 17, 
  504.             windowRect.top         + 3,
  505.             windowRect.right     - 3,
  506.             windowRect.top         + 17
  507.         );
  508.         buttonState = !buttonState;
  509.         if (drawColor)
  510.             ToggleZoom(thisRect, pat, buttonState, drawColor);
  511.         else
  512.             InvertRect(&thisRect);
  513.         
  514.         break;
  515.         
  516.     default:
  517.         break;
  518.         
  519.     }
  520. }
  521.